package com.xzr.yespaper.controller;

import com.xzr.yespaper.bean.Files;
import com.xzr.yespaper.service.FilesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;


@Controller
public class ImageController {

    @Autowired
    FilesService filesService;

    /**
     * Upload User photo
     * @param file image
     * @param session HttpSession
     * @return JSONObject {code: true/false} upload successful or not
     * @throws JSONException 没用
     * @throws IOException 没用
     */
    @PostMapping("/uploadimg")
    @ResponseBody
    public String mainPage(@RequestParam("avatar1") MultipartFile file, HttpSession session) throws JSONException, IOException {
        String uuid = UUID.randomUUID().toString();
        // 设置路径 Set save path (存储到编译后的文件中)
        String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        file.transferTo(new File(path + "static/files/"+uuid+".png"));
        Files fileRecord = new Files();
        fileRecord.setAddress(uuid);
        Object userid = session.getAttribute("userid");
        fileRecord.setRelatedid(Long.parseLong(userid.toString()));
        System.out.println(fileRecord.getRelatedid());
        if(filesService.getFilesByFirstID(fileRecord.getRelatedid()).size()==0){
            // 创建 Creat if first time
            filesService.creatFiles(fileRecord);
        }else{
            // 更新 Update if exits
            filesService.updateFilesAddress(fileRecord);
        }
        // System.out.println(file);
        JSONObject ret = new JSONObject();
        ret.put("code",true);
        return ret.toString();
    }

    /**
     * 得到头像 Get User photo (address) by user id
     * @param session HttpSession
     * @return JSONObject {code: true/false} successful or not
     * @throws JSONException 没用
     */
    @PostMapping("/getuserimg")
    @ResponseBody
    public String getimg(HttpSession session) throws JSONException {
        // get user id from session
        Long userid = Long.parseLong(session.getAttribute("userid").toString());
        List<Files> files = filesService.getFilesByFirstID(userid);
        if(files.size()==0){
            JSONObject ret = new JSONObject();
            ret.put("code",false);
            return ret.toString();
        }else{
            JSONObject ret = new JSONObject();
            ret.put("code",true);
            ret.put("img",files.get(0).getAddress());
            return ret.toString();
        }
    }

    /**
     * 页面跳转到修改头像页 To upload photo page
     * @return imgdemo.html
     */
    @GetMapping("/imguploadpage")
    public String a(){
        return "imgdemo";
    }
}
